package genedit.controller.interfaceActions;

import genedit.gui.GToolbar;
import genedit.model.Workspace;
import genedit.model.graph.GraphElement;
import genedit.view.DocumentView;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.KeyStroke;

/**
 * Akcija zadužena za pretragu grafičkih elemenata po nazivu.
 * 
 * @author Nikola
 * @version 1.0 5/1/14
 */
public class GSearchA extends AbstractAction {

	private static final long serialVersionUID = 1L;
	private static GSearchA instance;

	public GSearchA() {
		putValue(NAME, "Search");
		putValue(SHORT_DESCRIPTION, "Search");
		putValue(MNEMONIC_KEY, KeyEvent.VK_L);
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control H"));
	}

	public static GSearchA getInstance() {
		if (instance == null)
			instance = new GSearchA();
		return instance;
	}

	/**
	 * Akcija pretražuje sve elemente aktivnog dokumenta po njihovom nazivu i
	 * ubacuje ih u selekciju, nakon čega pozicionira vizir tako da obuhvati sve
	 * selektovane elemente.
	 */
	public void actionPerformed(ActionEvent arg0) {
		DocumentView document = Workspace.getInstance().getActiveDocument();
		if (document == null)
			return;

		document.getModel().getSelectedElements().clear();
		for (GraphElement ge : document.getModel().getElements()) {
			if (ge.getName().contains(GToolbar.getInstance().getSearchTextField().getText().trim()))
				document.getModel().addSelectedElements(ge);
		}

		if (document.getModel().getSelectedElements().size() > 0)
			GZoomBestFit.getInstance().actionPerformed(arg0);
	}

}